package com.lanchetech.admin.controller;

import com.lanchetech.admin.aop.OperatorLog;
import com.lanchetech.bean.request.CardConsumptionFundItemReq;
import com.lanchetech.bean.request.CardConsumptionFundReq;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.bean.vo.CardConsumptionFundItemVo;
import com.lanchetech.bean.vo.CardConsumptionFundVo;
import com.lanchetech.common.enums.AccessType;
import com.lanchetech.common.exception.ResultCodeException;
import com.lanchetech.entity.CardConsumptionFund;
import com.lanchetech.service.CardConsumptionFundService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/api/consumption/fund")
@Api(value = "消费金管理", tags = "消费金管理")
public class AdminCardFundController {

    @Autowired
    private CardConsumptionFundService cardConsumptionFundService;

    @PostMapping("/save")
    @ApiOperation("创建,修改 消费金")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cardCode", value = "卡号", dataType = "String", paramType = "body", required = true),
            @ApiImplicitParam(name = "cardQuota", value = "卡额度", dataType = "Double", paramType = "body", required = true),
            @ApiImplicitParam(name = "cardImage", value = "卡背景图", dataType = "String", paramType = "body", required = true),
            @ApiImplicitParam(name = "cardRemark", value = "卡介绍", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "cardName", value = "卡名称", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "id", value = "带ID是修改", dataType = "Long", paramType = "body"),
    })
    @OperatorLog("创建,修改 消费金")
    public BaseResp save(@RequestBody CardConsumptionFund cardConsumptionFund) {
        BaseResp baseResp = new BaseResp();
        try {
            int id = cardConsumptionFundService.save(cardConsumptionFund);
        } catch (ResultCodeException e) {
            baseResp.setResultCode(e.getResultCode());
        }
        return baseResp;
    }

    @PostMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "分页页码,从1开始", dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "pageSize", value = "每页页数", dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "cardCode", value = "卡号", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "cardName", value = "卡名称", dataType = "String", paramType = "body")
    })
    @ApiOperation("消费金列表")
    public ResultData<BasePageResp<CardConsumptionFundVo>> list(@RequestBody CardConsumptionFundReq cardConsumptionFundReq) {
        ResultData<BasePageResp<CardConsumptionFundVo>> vo = cardConsumptionFundService.list(cardConsumptionFundReq);
        return vo;
    }

    @GetMapping("/applySpu")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "分页页码,从1开始", dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "pageSize", value = "每页页数", dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "cardConsumptionFundId", value = "消费金ID", dataType = "Long", paramType = "body", required = true),
            @ApiImplicitParam(name = "status", value = "0:待审核 1:审核通过 2:审核拒绝", dataType = "Byte", paramType = "body"),
            @ApiImplicitParam(name = "spuId", value = "商品编号", dataType = "Long", paramType = "body")
    })
    @ApiOperation("查看商家提交商品")
    public ResultData<BasePageResp<CardConsumptionFundItemVo>> applySpu(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                        @RequestParam(name = "cardConsumptionFundId") Long cardConsumptionFundId,
                                                                        @RequestParam(name = "status", required = false) Byte status,
                                                                        @RequestParam(name = "spuId", required = false) Long spuId) {
//        ResultData<BasePageResp<CardConsumptionFundItemVo>> vo = cardConsumptionFundService.applySpu(req);
        CardConsumptionFundItemReq req = new CardConsumptionFundItemReq();
        req.setSpuId(spuId);
        req.setPageSize(pageSize);
        req.setPageNo(pageNo);
        req.setCardConsumptionFundId(cardConsumptionFundId);
        req.setAccessType(AccessType.TERRACE);
        req.setStatus(status);
        return cardConsumptionFundService.getBindSpu(req);
    }

    @PostMapping("/examineSpu")
    @ApiOperation("审核商家商品")
    @OperatorLog("审核商家商品")
    public BaseResp examineSpu(@RequestBody CardConsumptionFundItemReq req) {
        BaseResp baseResp = new BaseResp();
        try {
            cardConsumptionFundService.examineSpu(req);
        } catch (ResultCodeException e) {
            baseResp.setResultCode(e.getResultCode());
        }
        return baseResp;
    }

    @GetMapping("/whether")
    @ApiOperation("开启/关闭")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "消费金标识", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "whether", value = "开启状态 true 开启,false 关闭", dataType = "Boolean", paramType = "query", required = true)
    })
    @OperatorLog("消费金开启/关闭")
    public BaseResp whether(@RequestParam Long id, @RequestParam boolean whether) {
        BaseResp baseResp = new BaseResp();
        try {
            cardConsumptionFundService.whether(id, whether);
        } catch (ResultCodeException e) {
            baseResp.setResultCode(e.getResultCode());
        }
        return baseResp;
    }
}
